//
//  ListenXqViewController.m
//  news
//
//  Created by dllo on 16/3/17.
//  Copyright © 2016年 imacbookair. All rights reserved.
//

#import "ListenXqViewController.h"
@interface ListenXqViewController ()<UITableViewDataSource, UITableViewDelegate>
@property (nonatomic, strong)UITableView *tableView;
@property (nonatomic, strong)NSMutableArray *dataArr;
@property (nonatomic, strong)ListenModel *listenModel;
@property(nonatomic, strong)NSMutableArray *soundDataArr;//声音数据
@property (nonatomic, strong)UIView  *topView; // 上部视图
@property (nonatomic, strong)NSMutableArray *arrTemp;
@property (nonatomic, strong)UIImageView *headImg;// 小图片
@end

@implementation ListenXqViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = _model.title;
   
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 160, self.view.frame.size.width, self.view.frame.size.height - 160 - 64) style:UITableViewStylePlain];
//    self.tableView.backgroundColor = [UIColor redColor];
    [self.view addSubview:_tableView];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    self.tabBarController.tabBar.hidden = YES;
    [_tableView registerClass:[ListenXqTableViewCell class] forCellReuseIdentifier:@"cell"];
    
    /** 数据请求 **/
    NSString *str =@"http://mobile.ximalaya.com/mobile/others/album/track?position=1&albumId=";
    NSString *strOne = @"&source=1&isAsc=true&subtitle=%E6%9C%80%E7%81%AB&device=android&title=%E7%B2%BE%E9%80%89%7C%E6%AD%8C%E5%8D%95&pageSize=20";
    NSString *strTow = [NSString stringWithFormat:@"%@%@%@", str, _model.albumId, strOne];
    [NewWorking GetDayaWithURL:strTow dic:nil sussess:^(id responseObject) {
        self.dataArr = [PlayXqModel modelHanderWithArray:responseObject[@"tracks"][@"list"]];
        _arrTemp = [PlayXqModel modelHanderWithArray:responseObject[@"tracks"][@"list"]];
        
        [_tableView reloadData];
       
    } filed:^(NSError *error) {
        
    }];
    
            //铺设视图
        [self viewSet];
    
}
- (void)viewSet{
    self.topView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 160)];
    _topView.backgroundColor = [UIColor colorWithRed:50 / 225.0 green:30 / 225.0 blue:39 / 255.0 alpha:1.0];
    [self.view addSubview:_topView];
    
    UIImageView *mainImg = [[UIImageView alloc] initWithFrame:CGRectMake(20, 30, 100, 100)];
    //    imgView.backgroundColor = [UIColor cyanColor];
    [_topView addSubview:mainImg];
    [mainImg sd_setImageWithURL:[NSURL URLWithString:_model.albumCoverUrl290] placeholderImage:[UIImage imageNamed:@"musicSmall"]];
    
    _headImg = [[UIImageView alloc] initWithFrame:CGRectMake(130, 30, 30, 30)];
    _headImg.layer.cornerRadius = 15;
    _headImg.clipsToBounds = YES;
    
    [_topView addSubview:_headImg];
   
    UILabel *nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(170, 30, 150, 30)];
    [_topView addSubview:nameLabel];
    nameLabel.text = _model.intro;
    nameLabel.textColor = [UIColor whiteColor];
    
    UILabel *introLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 70, 150, 20)];
    [_topView addSubview:introLabel];
    introLabel.text =_model.lastUptrackTitle;
    introLabel.textColor = [UIColor whiteColor];
    introLabel.font = [UIFont systemFontOfSize:15];
    
    UILabel *tagsLabel = [[UILabel alloc] initWithFrame:CGRectMake(130, 100, 200, 30)];
    [_topView addSubview:tagsLabel];
    tagsLabel.text = _model.tags;
    tagsLabel.textColor = [UIColor whiteColor];
    tagsLabel.font = [UIFont systemFontOfSize:15];
}
#pragma mark - tableView设置
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArr.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ListenXqTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    PlayXqModel *m = _arrTemp[indexPath.row];
    cell.titleLabel.text = m.title;
    if ([m.playtimes floatValue] > 100000000) {
        NSString *str = [NSString stringWithFormat:@"%.1f亿", [m.playtimes floatValue] / 100000000];
        cell.playtimesLabel.text = str;
    }else if ([m.playtimes floatValue] > 10000){
        NSString *str = [NSString stringWithFormat:@"%.1f万", [m.playtimes floatValue] / 10000];
        cell.playtimesLabel.text = str;
    }else {
        cell.playtimesLabel.text = [NSString stringWithFormat:@"%@", m.playtimes];
    }
    [_headImg sd_setImageWithURL:[NSURL URLWithString:m.coverLarge]];
    NSString *str = [NSString stringWithFormat:@"%02ld:%.02ld", [m.duration integerValue] / 60, [m.duration integerValue] % 60];
    cell.durationLabel.text = str;
    
    cell.commentsLabel.text = [m.comments stringValue];
    
    return cell;
    
}
#pragma mark - cell 高度
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    PlayXqModel *m = _dataArr[indexPath.row];
    CGFloat h = [ListenXqTableViewCell heightWithText:m.title width:self.view.frame.size.width - 20];
    return 40 + h;
}

#pragma mark - cell 点击方法

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    DataViewController *dada = [[DataViewController alloc]init];
    
    dada.playModel = _arrTemp[indexPath.row];
    
    [self.navigationController presentViewController:dada animated:YES completion:^{
        
    }];
    
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
